package com.five.taotao.guide;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.util.StatusBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GuideActivity extends AppCompatActivity {

    @BindView(R.id.viewpageRadiogroup)
    RadioGroup radioGroup;

    private HomeFragment homeFragment;
    private ClassifyFragment classifyFragment;
    private SocialFragment socialFragment;
    private ShoppingCartFragment shoppingCartFragment;
    private MineFragment mineFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.setWindowStatusBarColor(this, Color.WHITE);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initData();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

                switch (checkedId) {
                    case R.id.item1:
                        ft.show(homeFragment).hide(classifyFragment).hide(socialFragment).hide(shoppingCartFragment).hide(mineFragment).commit();
                        break;
                    case R.id.item2:
                        ft.show(classifyFragment).hide(homeFragment).hide(socialFragment).hide(shoppingCartFragment).hide(mineFragment).commit();
                        break;
                    case R.id.item3:
                        ft.show(socialFragment).hide(shoppingCartFragment).hide(homeFragment).hide(classifyFragment).hide(mineFragment).commit();
                        break;
                    case R.id.item4:
                        ft.show(shoppingCartFragment).hide(homeFragment).hide(classifyFragment).hide(socialFragment).hide(mineFragment).commit();
                        break;
                    case R.id.item5:
                        ft.show(mineFragment).hide(homeFragment).hide(classifyFragment).hide(socialFragment).hide(shoppingCartFragment).commit();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    /**
     * 初始化数据信息，
     * 将Fragment 添加到ViewPager 上面去
     */
    public void initData() {
        homeFragment = new HomeFragment();
        classifyFragment = new ClassifyFragment();
        socialFragment = new SocialFragment();
        shoppingCartFragment = new ShoppingCartFragment();
        mineFragment = new MineFragment();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container, homeFragment)
                .add(R.id.container, classifyFragment)
                .add(R.id.container,socialFragment)
                .add(R.id.container, shoppingCartFragment)
                .add(R.id.container, mineFragment)
                .commit();

        getSupportFragmentManager().beginTransaction()
                .show(homeFragment).hide(classifyFragment).hide(socialFragment).hide(shoppingCartFragment).hide(mineFragment).commit();

    }


    private static boolean isExit = false;
    //按两次退出
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    //物理按键点击方法
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            //利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 1500);//两秒之后更改 isExit=false;
        } else {
            finish();//结束当前Activity
            System.exit(0);//退出程序
        }
    }
}
